package by.jtraining.london.web.action;

import static by.jtraining.london.constants.Constants.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import by.jtraining.london.model.Course;
import by.jtraining.london.model.Lector;
import by.jtraining.london.service.LectorService;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.opensymphony.xwork2.Action;

public final class AdminAction  implements ServletRequestAware, ServletResponseAware,
ServletContextAware, SessionAware{

	private HttpServletRequest request;
	private HttpServletResponse response;
	private ServletContext context;
	private Map<String, Object> session;
	
	private List<Lector> lectorList = new ArrayList<Lector>();
	private Lector currentLector;
	
	@Inject
	@Named("courseList")
	private List<String> courseList;
	
	@Inject
	@Named("educationList")
	private List<String> educationList;
	
	private final LectorService lectorService;
	
	public String trainerReverseMarkActive(){
		lectorService.reverseIsActiveForLector(Long.parseLong(request.getParameter(ID)));
		lectorList = lectorService.getAllLectors();
		return Action.SUCCESS;
	}
	
	public String viewConcreteTrainer(){
		currentLector = lectorService.getById(Long.parseLong(request.getParameter(ID)));
		return Action.SUCCESS;
	}
	public String updateTrainer(){
		if(request.getParameter(ID) != null){
			currentLector = lectorService.getById(Long.parseLong(request.getParameter(ID)));
			currentLector.setFirstName(request.getParameter(FIRST_NAME));
			currentLector.setLastName(request.getParameter(LAST_NAME));
			currentLector.setCourse(Course.getCourse(Integer.parseInt(request.getParameter(COURSE))));
			currentLector.setIsActive(Boolean.valueOf(request.getParameter(IS_ACTIVE)));
			lectorService.save(currentLector);
		}else{
			lectorService.add(new Lector(
					request.getParameter(FIRST_NAME),
					request.getParameter(LAST_NAME),
					request.getParameter(LOGIN),
					"123",
					Course.getCourse(Integer.parseInt(request.getParameter(COURSE)))
					));
		}
		lectorList = lectorService.getAllLectors();
		return Action.SUCCESS;
	}	
	public String addTrainer(){
		return Action.SUCCESS;
	}	
	public String prepareTrainingList(){
		lectorList = lectorService.getAllLectors();
		return Action.SUCCESS;
	}	
	
	public Lector getCurrentLector() {
		return currentLector;
	}
	public void setCurrentLector(Lector currentLector) {
		this.currentLector = currentLector;
	}
	public List<String> getCourseList() {
		return courseList;
	}
	public void setCourseList(List<String> courseList) {
		this.courseList = courseList;
	}

	public List<String> getEducationList() {
		return educationList;
	}
	public void setEducationList(List<String> educationList) {
		this.educationList = educationList;
	}
	
	@Inject
	public AdminAction(LectorService lectorService){
		this.lectorService = Preconditions.checkNotNull(lectorService);
	}
	
	public List<Lector> getLectorList() {
		return lectorList;
	}
	public void setLectorList(List<Lector> lectorList) {
		this.lectorList = lectorList;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	@Override
	public void setServletContext(ServletContext context) {
		this.context = context;
	}
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public ServletContext getContext() {
		return context;
	}
	public Map<String, Object> getSession() {
		return session;
	}

}
